<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\CourseBundle\Model;
use Base\BaseModel;

class CourseLessonContentModel extends BaseModel{


    public function getOneArray($id,$courseId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("id = ?")
            ->setParameter(0,$id)
            ->andWhere("courseId = ?")
            ->setParameter(1,$courseId)
            ->from("CourseLessonContent");
        return $qb->execute()->fetch();
    }

    public function getListForCourse($courseId)
    {

    }

    public function getListForCourseArrange($arrangeId,$courseId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("arrangeId = ?")
            ->setParameter(0,$arrangeId)
            ->andWhere("courseId = ?")
            ->setParameter(1,$courseId)
            ->from("CourseLessonContent");
        return $qb->execute()->fetchAll();
    }

    public function commWebAdd($params)
    {
        $default = array(
            "courseId"  => 0,
            "arrangeId"  => 0,
            "title"  => "章节标题未设置",
            "orders"=> 0,
            "addTime"=> time(),
            "updateTime"=> time(),
            "info"  => "章节简介未设置",
            "type"  => 0,
            "status"  => 0,
            "content"  => "课时内容未设置",
        );
        foreach ($default as $k => &$v) {
            if (isset($params[$k])) {
                $v = $params[$k];
            }
        }

        if (isset($params["id"])) {
            //编辑
            $qb = $this->getQueryBuilder();
            $qb->update("CourseLessonContent");
            $qb->where("id = :id")->setParameter("id",$params["id"]);
            foreach ($default as $key => $val) {
                $qb->set("$key",":$key")->setParameter(":$key",$val);
            }
        } else {
            //新增
            $qb = $this->getQueryBuilder();
            $qb->insert("CourseLessonContent");
            $keys = array_keys($default);
            $values = array_values($default);
            $insertKeys = array();
            foreach ($keys as $k => $val) {
                $insertKeys[$val] = "?";
            }
            $qb->values($insertKeys)->setParameters($values);
        }
        $qb->execute();
        if ( $qb->getState() === 1) {
            return $default;
        } else {
            return $qb->getState();
        }
    }


    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function countCourseLesson($courseId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("count(id) as num")
            ->from("CourseLessonContent")
            ->where("courseId = ?")->setParameter(0,$courseId);
        $rst  = $qb->execute()->fetch();
        return $rst["num"];
    }
}